package wt.hpad.activity.main;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import wt.hpad.R;
import wt.hpad.utils.ActivityUtil;
import wt.hpad.utils.AppUtil;

import static wt.hpad.utils.LogUtil.logI;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.main_content)
    FrameLayout mainContent;
    @BindView(R.id.drawlayout_main)
    DrawerLayout drawlayoutMain;
    @BindView(R.id.nav_main)
    NavigationView navMain;
    private MainPresenter mMainPresenter;
    private Unbinder bind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_act);
        logI("MainActivity", "onCreate |  ", "" + "");
        initUI();
        initData();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        logI("MainActivity", "onRestart |  ", "" + "");
    }

    @Override
    protected void onResume() {
        super.onResume();
        logI("MainActivity", "onResume |  ", "" + "");
    }

    private void initData() {
        MainFragment mainFragment = new MainFragment();
        ActivityUtil.addFragmentToActivity(getSupportFragmentManager(), mainFragment, R.id.main_content);
        mMainPresenter = new MainPresenter(mainFragment, this, getApplicationContext());
        mainFragment.setPresenter(mMainPresenter);
    }

    private void initUI() {
        AppUtil.hideBarAndTransparentTime(this);
//        AppUtil.hideBottomUIMenuAll(this);
        bind = ButterKnife.bind(this);
        // 绑定 NavigationView 点击事件的处理类
        navMain.setNavigationItemSelectedListener(this);

        toolbar.setTitle("点菜");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.mipmap.ic_menu);
        }

    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        drawlayoutMain.closeDrawers();
        switch (item.getItemId()) {

            case R.id.nav_update_table:
                mMainPresenter.updateTableInfo(this);
                break;
            case R.id.nav_update_menu:
                mMainPresenter.updateMenu(this);
                break;
//            case R.id.nav_set_deviceID: // 暂时去掉le
//                mMainPresenter.setDeviceID();
//                break;
            case R.id.nav_set_server:
                mMainPresenter.setServerInfo();
                break;
            case R.id.nav_set_showItem:
                mMainPresenter.setShowItem();
                break;
            case R.id.nav_exit:
                onDestroy();
                break;
//            case R.id.nav_test: // 仅供测试
//                Test.initTestData();
//                Test.startOrder();
//                break;
        }
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawlayoutMain.openDrawer(GravityCompat.START);
                break;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        logI("MainActivity", "onDestroy |  ", "" + "");
        mMainPresenter.unsubscribe();
        bind.unbind();
        this.finish();
        System.exit(0);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        onDestroy();
    }
}
